package com.fpx.order.plat.bussiness.service.log.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fpx.order.plat.bussiness.dao.log.IPullOrderLogDao;
import com.fpx.order.plat.bussiness.entity.PullOrderLog;
import com.fpx.order.plat.bussiness.service.log.IPullOrderLogService;
import com.fpx.order.plat.bussiness.vo.PullOrderLogVo;
import com.fpx.order.plat.web.utils.PageUtil;

/**
 * 拉单日志Service实现类
 * desc: PullOrderLogServiceImpl
 * @author xiangtao(xiangtao@4px.com)
 * @date 2015年12月7日 下午6:11:38
 * @version 1.0
 */
@Service("pullOrderLogService")
public class PullOrderLogServiceImpl implements IPullOrderLogService{
	
	@Autowired
	private IPullOrderLogDao pullOrderLogDao;
	
	@Override
	public PullOrderLog findLastSyncTime(String cm_id, String cs_id,
			String plat_code) throws Exception {
		PullOrderLog pullOrderLog= new PullOrderLog();
		pullOrderLog.setFk_cm_id(cm_id);
		pullOrderLog.setFk_cs_code(cs_id);
		pullOrderLog.setCs_platform_code(plat_code);
		return pullOrderLogDao.findLastSyncTime(pullOrderLog);
	}

	@Override
	public Map<String, Object> findPullOrderLogPage(Map<String, Object> map)
			throws Exception {
		if(map==null){
			throw new Exception("分页请求参数不能为空");
		}
		try{
			if(!StringUtils.isNumeric((String)map.get("page"))||
					!StringUtils.isNumeric((String)map.get("rows"))){
				throw new Exception("分页参数page,rows只能为数字");
			}
		}catch(Exception e){
			throw new Exception("分页请求参数不正确");
		}
		//当前页
		int page=Integer.parseInt((String)map.get("page"));
		//每页行数
		int rows=Integer.parseInt((String)map.get("rows"));
		//开始查询的行数
		int start_no=PageUtil.getStartNo(page, rows);
		map.put("start_no", String.valueOf(start_no));
		//结束查询的行数
		int end_no=PageUtil.getEndNo(rows, start_no);
		map.put("end_no", String.valueOf(end_no));
		
		//根据条件分页查询数据
		List<PullOrderLogVo> originOrderLst=pullOrderLogDao.findPagePullOrderLog(map);
		//根据条件查询总行数
		int records=pullOrderLogDao.findCountPullOrderLog(map);
		map.put("rows", originOrderLst);//变成数据集了
		map.put("records", records);//总行数
		map.put("total",PageUtil.getPageSize(records, rows));//总页数
		return map;
	}
}

